package com.itheima.listener;

import com.itheima.domain.mongo.VideoScore;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(topic ="tanhua-recommend-video" ,consumerGroup ="recommendVideo" )
public class VideoScoreListener implements RocketMQListener<Map> {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onMessage(Map map) {
        System.out.println("接收小视频消息"+map);
        Long userId = Long.valueOf(map.get("userId").toString());
        Long vid = Long.valueOf(map.get("vid").toString());
        Integer type = Integer.valueOf(map.get("type").toString());

        VideoScore videoScore=new VideoScore();
        videoScore.setDate(System.currentTimeMillis());
        videoScore.setUserId(userId);
        videoScore.setVideoId(vid);
        // 计算分值
        switch (type) {
            case 1: {
                videoScore.setScore(20d);
                break;
            }
            case 2: {
                videoScore.setScore(5d);
                break;
            }
            case 3: {
                videoScore.setScore(-5d);
                break;
            }
            case 4: {
                videoScore.setScore(10d);
                break;
            }
        }
        mongoTemplate.save(videoScore);
    }
}
